//
//  PJClient.h
//  NG911
//
//  Created by Rachid Jeitani on 10/17/11.
//  Copyright (c) 2011 Columbia University. All rights reserved.
//


#ifndef _PJCLIENT_H_
#define _PJCLIENT_H_

#import <Foundation/Foundation.h>
#import "Message.h"
#import <pjsua-lib/pjsua.h>
#import <pjlib.h>
#import <pjlib-util.h>
#import <pjmedia.h>
#import <pjmedia-codec.h>
#import <pjsip.h>
#import <pjsip_simple.h>
#import <pjsip_ua.h>
#import <pjsip-ua/sip_inv.h>

#import  <ifaddrs.h>
#import <arpa/inet.h>
#import <sys/types.h>
#import <sys/socket.h>
#import <net/if.h>
#import <net/if_dl.h>

// PJClientMessageDelegate

@protocol PJClientMessageDelegate <NSObject>

@optional
-(void) messageReceivedFrom:(NSString *)sender WithContent:(id)content;
-(void) typingIndicatorReceived;
-(void) realtimeMessageReceivedwithContent:(NSString *)content;
-(void) realtimeBackspaceReceived;
-(void) realtimeReturnReceived;

@required
-(void)didFinishLoadingWithStatus:(int) status;

@end



@interface PJClient : NSObject

{
    id <PJClientMessageDelegate> delegate;

}

+(PJClient *) sharedPJClient; // returns singleton instance

@property (retain) id<PJClientMessageDelegate> delegate;
@property (nonatomic, retain) CLLocation *currentLocation;
@property (nonatomic, retain) NSString *pidfString;

// Obj-C Interfaces


-(void) initClientWithLocation:(CLLocation *)location;
-(int) sendMsg:(Message *)msg to:(NSString*) dest;
-(void) charTyped:(const char *)str;
-(void) destroy;

+(NSString *) addressForInterface:(NSString *) interface;
+(NSString *) localWifiAddress;
+(NSString *) localCellAddress;
+(NSString *) localAddress;


// C functions

void sendMsg(const char* chat_dest_addr, const char* msg, char* type, int tcp);
void receiveMsg(pjsua_call_id call_id, const pj_str_t *from, const pj_str_t *to, const pj_str_t *contact, const pj_str_t *mime_type, const pj_str_t *text);


// RTP/RTT functions

static int rtp_thread (void *);
void on_rx_rtp (void *user_data, void *pkt, pj_ssize_t size);
void on_rx_rtcp(void *user_data, void *pkt, pj_ssize_t size);

pj_status_t createSDP(pjmedia_sdp_session **p_sdp);
pj_status_t sendInvite(const pj_str_t *dst_uri);
pj_status_t createMultipartMessageBody(pjsip_msg_body **msg);

void insertRTTchar(const char *);
static pj_bool_t on_rx_request( pjsip_rx_data *rdata );

// rtt callbacks
void call_on_media_update (pjsip_inv_session *inv, pj_status_t status);
void call_on_state_changed( pjsip_inv_session *inv, pjsip_event *e);
void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);



@end


#endif